package com.huatai.bpmn.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.huatai.bpmn.entity.CompleteConfigEntity;
import com.huatai.bpmn.entity.DeployFormMappingEntity;
import com.huatai.bpmn.entity.TbBusinessEntity;
import com.huatai.bpmn.service.CompleteConfigService;
import com.huatai.bpmn.service.DeployFormMappingService;
import com.huatai.bpmn.service.TbBusinessService;
import com.huatai.common.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author gusiyi
 * @version 1.0
 * @className CompleteController
 * @description 描述：正式数据controller
 * @date 2023/8/18 20:21
 */
@RestController
@RequestMapping("/complete")
@Api(value = "正式数据操作controller",tags = "正式数据操作controller")
public class CompleteController {

	@Autowired
	private CompleteConfigService completeConfigService;

	@Autowired
	private TbBusinessService tbBusinessService;

	@Autowired
	private DeployFormMappingService deployFormMappingService;

	@PostMapping("/saveOrUpdate")
	@ApiOperation(value = "存储或修改正式数据表单配置")
	@ApiOperationSupport(includeParameters = {
		"completeConfigEntity.id",
		"completeConfigEntity.formName",
		"completeConfigEntity.dataConfig",
		"completeConfigEntity.paramConfig",
		"completeConfigEntity.filesConfig"
	})
	public R<Boolean> saveOrUpdate(@RequestBody CompleteConfigEntity completeConfigEntity){
		return R.data(completeConfigService.saveOrUpdate(completeConfigEntity));
	}

	@GetMapping("/page")
	@ApiOperation(value = "获取正是数据配置清册")
	public R<Page<CompleteConfigEntity>> page(@RequestParam("pageIndex") Integer pageIndex,@RequestParam("pageSize")Integer pageSize){
		return R.data(completeConfigService.page(new Page<>(pageIndex,pageSize)));
	}



	@GetMapping("/getCompleteData")
	@ApiOperation(value = "获取正式数据")
	public R<Map<String,Object>> getCompleteData(@ApiParam(name = "configId",value = "配置id",required = true)  @RequestParam("configId")Long configId,
												 @ApiParam(name = "processInstanceId",value = "业务流水号",required = true)  @RequestParam("processInstanceId")String processInstanceId){
		return R.data(completeConfigService.getCompleteData(configId,processInstanceId));
	}

	@DeleteMapping("/delete")
	@ApiOperation(value = "删除正式数据表单配置")
	public R<Boolean> delete(@ApiParam(name = "configId",value = "配置id",required = true) @RequestParam("configId")Long configId){
		return R.data(completeConfigService.removeById(configId));
	}

	@PostMapping("/getCompleteBusinessList")
	@ApiOperation(value = "获取已完成的事项数据")
	@ApiOperationSupport(includeParameters = {
		"tbBusinessEntity.deployId",
		"tbBusinessEntity.pageIndex",
		"tbBusinessEntity.pageSize"
	})
	public R<Page<TbBusinessEntity>> getCompeleteBusinessList(@RequestBody TbBusinessEntity tbBusinessEntity){
		LambdaQueryWrapper<TbBusinessEntity> businessQuery = Wrappers.lambdaQuery();
		businessQuery
			.select(TbBusinessEntity::getProcinstId,TbBusinessEntity::getDeployId,TbBusinessEntity::getMatterName,TbBusinessEntity::getCreateTime,TbBusinessEntity::getCreateUserid)
			.eq(TbBusinessEntity::getDeleted,false)
			.eq(TbBusinessEntity::getStatus,true)
			.eq(TbBusinessEntity::getDeployId,tbBusinessEntity.getDeployId())
			.orderByDesc(TbBusinessEntity::getUpdateTime);
		Page<TbBusinessEntity> page = new Page<>(tbBusinessEntity.getPageIndex(), tbBusinessEntity.getPageSize());
		tbBusinessService.page(page,businessQuery);

		LambdaQueryWrapper<DeployFormMappingEntity> deployFormMappingQuery = Wrappers.lambdaQuery();
		deployFormMappingQuery
			.select(DeployFormMappingEntity::getId,DeployFormMappingEntity::getCompleteId);
		return R.data(page);
	}

	@PostMapping("/bindCompleteForm")
	@ApiOperation(value = "绑定正式表单")
	public R<Boolean> bindCompleteForm(@RequestParam("deployId")String deployId,@RequestParam("completeFormKey")String completeFormKey){
		DeployFormMappingEntity deployFormMappingEntity = new DeployFormMappingEntity();
		deployFormMappingEntity.setCompleteId(completeFormKey);
		LambdaQueryWrapper<DeployFormMappingEntity> deployFormMappingQuery = Wrappers.lambdaQuery();
		deployFormMappingQuery
			.eq(DeployFormMappingEntity::getDeploymentid,deployId);
		return R.data(deployFormMappingService.update(deployFormMappingEntity,deployFormMappingQuery));
	}


}
